<?php
/**
 * Created by PhpStorm.
 * User: LiuZongqi
 * Date: 2021/1/5
 * Time: 11:38
 */

namespace app\admin\model;

use app\admin\validate\User as UserValidate;
use think\Model;

class User extends Model {
    protected $pk = 'id';
    //自动写入时间戳
    protected $autoWriteTimestamp = true;

    function add(){
        $name = input('post.name');
        $username = input('post.username');
        $password = input('post.password');
        $repassword = input('post.repassword');
        $validate = new UserValidate();
        $data = [
            'name' => $name,
            'username' => $username,
            'password' => $password,
            'repassword' => $repassword,
        ];
        if ($validate->scene('add')->check($data)){
            $data['password'] = qa_encrtyp($data['password']);
            $data['create_time'] = time();
            $data['update_time'] = $data['create_time'];

            $repeat = $this->db('user')->where('username',$username)->find();
            if (!$repeat){
                $result = $this->db('user')->strict(false)->data($data)->insert();
                if ($result){
                    return ['code' => true, 'msg' => '添加成功'];
                }else{
                    return ['code' => false, 'msg' => '添加失败'];
                }
            }else{
                return ['code' => false, 'msg' => '账号' . $username . '已存在'];
            }


        }else{
            return ['code' => false, 'msg' => $validate->getError()];
        }

    }
}